package api;
/* @Author      zouzh
 * @PackageName blog_system
 * @Package     api
 * @Date        2023/8/18 11:36
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null){
            // 用户本来就没登陆，谈不上注销，不过此处也没必要报错，直接跳转到登录也即可
            resp.sendRedirect("login.html");
            return;
        }
        // user 存在，就删除了，不存在，也不会有副作用
        session.removeAttribute("user");
        resp.sendRedirect("login.html");
    }
}
